$(function() {
    $.ajax({
        type: "post",
        contentType: "application/json",
        url: "/Article/address",
        data: "{}",
        success: function(result) {
            var str = '';
            for (var i = 0; i < result.length; i++) {
                str += '<option value="' + result[i]['sites_id'] + '">' + result[i]['name'] + '</option>';
            }
            $('#push_address').append(str);
        }
    });
    $('#push_address').change(function() {

        $.ajax({
            type: "post",

            url: "/Article/module",

            data: {
                "sites_id": $(this).val()
            },

            success: function(result) {
                $('#pushOne_Module option').remove();
                var str = '';
                for (var i = 0; i < result.length; i++) {
                    str += '<option value="' + result[i]['category_id'] + '" >' + result[i]['name'] + '</option>';
                }
                $('#pushOne_Module').append(str);
            },
            error: 　 function(err) {
                alert('error');
                console.log(err);
            }
        });
    });
    $('#pushOne_Module').change(function() {

        $.ajax({
            type: "post",

            url: "/Article/two",

            data: {
                "category_id": $(this).val()
            },

            success: function(result) {
                $('#pushTwo_Module option').remove();
                var str = '';
                for (var i = 0; i < result.length; i++) {
                    str += '<option value="' + result[i]['category_two_id'] + '">' + result[i]['name'] + '</option>';
                }
                $('#pushTwo_Module').append(str);
            },
            error: 　 function(err) {
                alert('error');
                console.log(err);
            }
        });
    });
});
$(document).ready(function() {
    var __ArtiForm = $('#form_article_add');
    var __editForm = $('#form_modal_edit');

    // $('.tableDelete').click(function(){
    $(document).on('click', '.tableDelete', function() {
        var userId = $(this).parent('td').attr('item_id'),
            thisUrl = $(this).attr('href');
        //官网欢迎页   
        layer.confirm('确认删除', {
            btn: ['确认', '取消'] //按钮
        }, function(index) {

            $.ajax({
                type: "GET",
                url: thisUrl,
                async: true,
                data: {
                    //"id" : userId
                },
                success: function(data) {
                    layer.alert(data.info, function() {
                        if (true === data.status) window.location = "/Article/list";
                    });
                }
            });
            layer.close(index);
            return false;

        }, function(index) {
            layer.close(index);
        });
        return false;

    });


    //$('.tableEdit').click(function(e){
    $(document).on('click', '.tableEdit', function(e) {

        //window.location = "/Article/edit";

    });


    $('#pushBtn').click(function(e) {
        $('#article_li').empty();
        $('#article_content').empty();
        $('#current_table input[name="checkBox"]:checked').each(function() {
            var arr = [];
            var tit = $(this).data('tit');
            var content = $(this).data('content');
            var id = $(this).data('id');
            $('#article_li').append($('<option data-id="' + id + '" value="' + id + '">' + tit + '</option>'));
            $('#article_content').append($('<option data-id="' + id + '" value="' + id + '">' + content + '</option>'));
            //alert('tit= '+tit+'\nid= '+id);
        });
        var checkBoxs = {

        }


    });

    $('#chose1').click(function() {
        $('#current_table input[name="checkBox"]').attr('checked', 'checked');
        $('#current_table input[name="checkBox"]').parent('span').addClass('checked');
        return false;
    })
    $('#chose2').click(function() {
        $('#current_table input[name="checkBox"]').each(function() {
            if ($(this).parent('span').hasClass('checked')) {
                $(this).removeAttr('checked');
                $(this).parent('span').removeClass('checked');
            } else {
                $(this).attr('checked', '');
                $(this).parent('span').addClass('checked');
            }

        })
        return false;
    })


    var exampleSocket = new WebSocket("ws://118.145.2.103:9501");
     exampleSocket.onopen = function(event) {
     exampleSocket.send("亲爱的服务器！我连上你啦！");
     };


   exampleSocket.onmessage = function(event) {
        console.log(event.data);
    }



    $(document).on('click', '#push_form', function() {
        var objArtiForm = {
            //__formName : $("input[name='name']", __editForm),
            __formName: $("select[name='push_address']", __editForm),
            __formOne: $("select[name='push_Module']", __editForm),
            __formTwo: $("select[name='pushModule']", __editForm),
            __formArticle: $("select[name='article_li']", __editForm),
            __formC: $("select[name='article_content']", __editForm)
        }

        articleId = document.getElementById('article_li');
        var idArr = {};
        for (i = 0; i < articleId.length; i++) {
            selectId = articleId.options[i].value;
            //alert(selectId);
            //idArr.push(selectId);
            idArr[i] = selectId;
        }

        var content = [];
        for (var i in objArtiForm) {

            if (objArtiForm[i].val() == '') {
                objArtiForm[i].css({
                    'border-color': 'red'
                });
                return false;
            } else {
                objArtiForm[i].css({
                    'border-color': '#e5e5e5'
                });
            }
            if (i != '__formArticle' && i != '__formC') {
                var selectContent = objArtiForm[i].find("option:selected").text();
            } else {
                var selectContent = objArtiForm[i].text();
            }
            content.push(selectContent);


        }
        $('#content').val(content);
        //alert(content);
        exampleSocket.send(content);

  
       
        $.ajax({
            type: 'POST',
            url: '/Article/pushTime',
            data: idArr,
            success: function(data) {
                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Article/list";
                });
            },
            error: function(err) {

            }
        })


    });

    $(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
        var urlOld = $(this).attr('href'),
            urlArr = urlOld.split("/");
        urlId = urlArr[urlArr.length - 1];
        $.ajax({
            type: 'POST',
            url: '/Article/page/page/' + urlId,
            data: urlId,
            success: function(data) {
                var data = $.parseJSON(data);

                $('#tbody').html('');
                var id = '';

                $.each(data, function(k, v) {

                    id = v['article_id'];
                    console.log("k" + k, "v" + v);

                    $('#tbody').append($('<tr class="gradeX"><td>' + v['title'] + '</td><td>' + v['name'] + '</td><td>' + v['author'] + '</td><td>' + v['source'] + '</td><td>' + v['article_status'] + '</td><td>' + v['current_time'] + '</td><td ><a class="tableDelete" href="/Article/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Article/edit/id/' + id + '">修改</a> </td></tr>'));

                });


            }

        });
        $.ajax({
            type: 'POST',
            url: '/Article/currentPage/page/' + urlId,
            data: urlId,
            success: function(data) {
                console.log(data);
                $('#pages').html(data);
                $('#pages').ready(function() {
                    nextBtn = $('#page_next');
                });
            }

        });
        return false;
    });



    $(".m-wrap").change(function() {
        //确定选中的是那个，得到他的value
        var $selectedvalue = $("input[name='push_style']:checked").val();
        if ($selectedvalue == 1) {
            //显示 下面的input            
            $("#push_module").show();
            $("#push_address").show();
            $("#push_time").hide();
        }
        if ($selectedvalue == 2) {
            $("#push_module").show();
            $("#push_address").show();
            $("#push_time").show();
        }
    });

});